/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.util;

import java.io.File;
import javax.swing.filechooser.FileFilter;
import java.util.List;

/**
 *
 * @author Gerben Feenstra
 */
public class FilePickerUtil {
    
    private FilePickerUtil(){}
            
     /**
     * Creates a filefilter for the specified List of Extensions
     * @param supportedExtensions The List of extensions in the form of .ext (for example: .dbf and .csv) this filefilter should support
     * @param formatDescription Description of the filter as shown in the selectionbox
     * @return 
     */
    
    public static FileFilter createSwingFileFilter(final List<String> supportedExtensions, final String formatDescription) {
        return new FileFilter() {

            @Override
            public boolean accept(File f) {
                if (f.isDirectory()) {
                    return true;
                }
                if (f.isFile()) {
                    String name = f.getName().toLowerCase();
                    for (String extention : supportedExtensions) {
                        if (name.endsWith(extention)) {
                            return true;
                        }
                    }
                }
                return false;
            }
                
            @Override
            public String getDescription() {
                return formatDescription;
            }
        };
    }
    
    /**
     * Create the display String for a filepicker. String in this form: "preceedingText (supportedExtensions String)" 
     * 
     * @param supportedExtensions List of supported extension to tranform to a String
     * @return String Example: "preceedingText (*.dbf, *.csv)"
     */
    public static String getFileFilterDescription(String preceedingText, List<String> supportedExtensions){
        StringBuilder sb = new StringBuilder();
        sb.append(preceedingText);
        sb.append(" ");
        sb.append(getFileFilterTypeString(supportedExtensions));
        return sb.toString();    
    }
    
    /**
     * Concatenates the List-items into one String surrounded with ( and )
     * Primary use = to build a file filter
     * 
     * @param toConcatenate List of Strings to concatenate
     * @return The String of concatenated String surrounded with ( and ). Example: (*.dbf, *.csv)
     */
    
    public static String getFileFilterTypeString(List<String> extensions){
        StringBuilder sb = new StringBuilder();
        sb.append("(");
        for(String extension : extensions){
            sb.append("*");
            sb.append(extension);
            sb.append(", ");
        }
        
        // remove last , plus its space
        if(sb.length() > 0){
            sb.delete(sb.length()-2, sb.length());
        }

        sb.append(")");
        return sb.toString();
    }
    
}
